package DAO;

import Utils.Buy;
import Utils.Sell;
import Utils.Stock;
import Utils.User;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Array;
import java.util.ArrayList;

@Repository
public class DealDAO
{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //提交购买数据
    public void Add_Buy(Buy buy) throws Exception
    {
        String sql = "Insert into Buy values(?,?,?,?,?,?)";

        Object[] args = new Object[]{buy.getBuyId(),buy.getUserID(),buy.getStockCode(),buy.getMaxPrice(),buy.getBuyCount(),null};

        jdbcTemplate.update(sql,args);
    }
    //提交出售数据
    public void Add_Sell(Sell sell) throws Exception
    {
        String sql = "Insert into Sell values(?,?,?,?,?,?)";

        Object[] args = new Object[]{sell.getSellId(),sell.getUserID(),sell.getStockCode(),sell.getLowestPrice(),sell.getSellCount(),null};

        jdbcTemplate.update(sql,args);
    }
    public ArrayList<ArrayList> Get_AllDeal(String UserId) throws Exception
    {
        ArrayList<ArrayList>objectArrayList = new ArrayList<>();

        //获取购买数据
        String buysql = "select * from Buy where UserId = ?";
        ArrayList<Buy> buy = null;

        buy = (ArrayList<Buy>) jdbcTemplate.query(buysql,BeanPropertyRowMapper.newInstance(Buy.class), new Object[]{UserId});

        objectArrayList.add(buy);
        //获取出售数据
        String sellsql = "select * from Sell where UserId = ?";
        ArrayList<Sell> sell = null;

        sell = (ArrayList<Sell>) jdbcTemplate.query(sellsql,new BeanPropertyRowMapper<Sell>(Sell.class),new Object[]{UserId});

        objectArrayList.add(sell);

        return objectArrayList;
    }
}
